package com.source.lightfowl.basedata.controller;

import com.source.lightfowl.VO.Barcode_GrantVO;
import com.source.lightfowl.VO.Barcode_RecoveryVO;
import com.source.lightfowl.basedata.entity.Barcode_Grant;
import com.source.lightfowl.basedata.entity.Barcode_Recovery;
import com.source.lightfowl.basedata.entity.Stalls;
import com.source.lightfowl.basedata.service.BarcodeService;
import com.source.lightfowl.basedata.service.StallsService;
import com.source.lightfowl.person.dao.PersonDao;
import com.source.lightfowl.person.entity.Person;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.annotation.After;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@CrossOrigin
@Api(tags = "条形码发放/回收管理")
public class BarcodeController {
    @Autowired
    private BarcodeService barcodeService;

    @Autowired
    private PersonDao personDao;

    @Autowired
    private StallsService stallsService;


    //条形码发放（测试通过）
    @ApiOperation(value = "条形码发放")
    @PostMapping("/barcodeGrant")
    @ResponseBody
    public MyResult BarcodeGrant(Barcode_Grant barcode_grant){
        try{
            barcodeService.saveBarcode_Grant(barcode_grant);
        } catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(1, "发放失败");
        }
        return MyResult.build(200, "发放成功");

    }

    //查看条形码发放（测试通过）
    @ApiOperation(value = "查看条形码发放")
    @GetMapping("/findBarcodeGrantAll")
    @ResponseBody
    public List<Barcode_GrantVO> FindBarcodeGrant(){
        List<Barcode_GrantVO> barcode_grantList= barcodeService.findBarcode_Grant();

        return barcode_grantList;
    }

    @ApiOperation(value = "分页查看条形码发放")
    @ResponseBody
    @GetMapping(value = "/allGrant/{page}/{size}")
    public MyResult findAllGrant(@PathVariable int page, @PathVariable int size){
        return MyResult.build(0, "查询成功",barcodeService.findPageGrant(page, size));
    }

    @ApiOperation(value = "回显条形码发放")
    @GetMapping("/findBarcodeGrantById")
    @ResponseBody
    public Barcode_GrantVO FindBarcodeGrantById(@RequestParam("id") String id){
        Barcode_GrantVO barcode_grant= barcodeService.findBarcode_GrantById(id);
        return barcode_grant;
    }

    //修改条形码发放（测试通过）
    @ApiOperation(value = "修改条形码发放")
    @PostMapping("/updateBarcodeGrant")
    @ResponseBody
    public MyResult UpdateBarcodeGrant(Barcode_Grant barcode_grant){
        try {
            barcodeService.updateBarcode_Grant(barcode_grant);
        } catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(1, "更新失败");
        }
        return MyResult.build(200, "更新成功");

    }

    //删除条形码发放（测试通过）
    @ApiOperation(value = "删除条形码发放")
    @DeleteMapping("/deleteBarcodeGrant")
    @ResponseBody
    @LogAnnotation(operEvent = "操作类型：删除",operType = 3)
    public MyResult DeleteBarcodeGrant(@RequestParam("id")  String id){
        try {
            barcodeService.deleteBarcode_Grant(id);
        } catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(1, "删除失败");
        }
        return MyResult.build(200, "删除成功");
    }

    //条形码回收（测试通过）
    @ApiOperation(value = "条形码回收")
    @PostMapping("/barcodeRecovery")
    @ResponseBody
    public void BarcodeRecovery(Barcode_Recovery barcode_recovery){
        barcodeService.saveBarcode_Recovery(barcode_recovery);
    }

    //查看条形码回收（测试通过）
    @ApiOperation(value = "查看条形码回收")
    @GetMapping("/findBarcodeRecovery")
    @ResponseBody
    public List<Barcode_RecoveryVO> FindBarcodeRecovery(){

        //条形码回收 两个属性
        List<Barcode_Recovery> list = barcodeService.findBarcode_Recovery();

        List<Barcode_RecoveryVO> collect = list.stream().map(s -> {
            //Barcode_GrantVO vo = new Barcode_GrantVO();
            Barcode_RecoveryVO vo = new Barcode_RecoveryVO();
            BeanUtils.copyProperties(s,vo);

            //Person user = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getGrantPerson()).longValue()));
            Person user = personDao.findVO(s.getRecoveryPerson());


            //
            if (user != null) {
                vo.setRecyclingName(user.getLoginName());
            }
            //Person user1 = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getReceivePerson()).longValue()));
            Person user1 = personDao.findVO(s.getSendbackPerson());
            //
            if (user1 != null) {
                vo.setSendName(user1.getLoginName());
            }

            //档口名
            String stallsId = s.getStallsId();
            Optional<Stalls> optional = stallsService.findById(Integer.valueOf(stallsId));
            if (optional.isPresent()){
                Stalls ss = optional.get();
                vo.setStallsName(ss.getStallsName());
            }


            return vo;
        }).collect(Collectors.toList());

        return collect;
    }

    @ApiOperation(value = "分页查询条形码回收全部数据")
    @GetMapping(value = "/allRecovery/{page}/{size}")
    public MyResult findAllRecovery(@PathVariable int page, @PathVariable int size){
        return MyResult.build(0, "查询成功",barcodeService.findPage(page, size));
    }

    //修改条形码回收（测试通过）
    @ApiOperation(value = "修改条形码回收")
    @PostMapping("/updateBarcodeRecovery")
    @ResponseBody
    @LogAnnotation(operEvent = "操作类型：修改",operType = 2)
    public MyResult UpdateBarcodeRecovery(Barcode_Recovery barcode_recovery){
        try {
            barcodeService.updateBarcodeRecovery(barcode_recovery);
        }catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(1, "修改失败");
        }
        return MyResult.build(200, "修改成功");
    }

    @ApiOperation(value = "回显条形码回收")
    @GetMapping("/findBarcodeRecoveryById")
    @ResponseBody
    public Barcode_RecoveryVO FindBarcodeRecoveryById(@RequestParam("id") String id){
        Barcode_RecoveryVO barcode_recovery= barcodeService.findBarcode_RecoveryById(id);
        return barcode_recovery;
    }

    //删除条形码发回收（测试通过）
    @ApiOperation(value = "删除条形码回收")
    @DeleteMapping("/deleteBarcodeRecovery")
    @ResponseBody
    @LogAnnotation(operEvent = "操作类型：删除",operType = 3)
    public MyResult DeleteBarcodeRecovery(@RequestParam("id")  String id){
        try {
            barcodeService.deleteBarcode_Recovery(id);
        }catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(1, "删除失败");
        }
        return MyResult.build(200, "删除成功");
    }

    //根据条形码对应时间段查询条形码数
    @ApiOperation(value = "条形码对应时间段查询条形码数")
    @GetMapping("/getGrantCountByTime")
    @ResponseBody
    public int getGrantCountByTime(@RequestParam("fromTime") String fromTime,@RequestParam("toTime") String toTime){
        return barcodeService.getGrantCountByTime(fromTime,toTime);
    }

}
